let app = getApp();
let API_URL = app.globalData.API_URL ;
import { getIdGlodShop, getGoldProduct } from "../../../../request/api.js";

let iconUrl = 'https://atush.vip/static/icons/';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        lang: null,
        shop_id: 0,
        goldShopCon: [],
        productList: [],
        filterShopGoods: [
            {id: 1, name: 'all', icon: iconUrl + 'quanbu.svg'},
            {id: 2, name: 'ring', icon: iconUrl + 'jiezhi.svg'},
            {id: 3, name: 'necklace', icon: iconUrl + 'xianglian.svg'},
            {id: 4, name: 'earrings', icon: iconUrl + 'erhuan.svg'},
            {id: 5, name: 'bracelet', icon: iconUrl + 'shouzhuo.svg'},
            {id: 6, name: 'watch', icon: iconUrl + 'shoubiao.svg'},
            {id: 7, name: 'suit', icon: iconUrl + 'taozhuang.svg'},
            {id: 8, name: 'goldPrice', icon: iconUrl + 'gold_price.svg'}
        ],
        filterActive: 0,
        show: false,
        isLike: false,
        filterType: -1,
        price_tab: false,
        price_tab_active: 0,
        selling_price: [
            {id: 1, price: 370, price_1: '380', date: '7.02'},
            {id: 2, price: 375, price_1: '380', date: '7.03'},
            {id: 3, price: 380, price_1: '380', date: '7.04'},
            {id: 4, price: 385, price_1: '380', date: '7.05'},
            {id: 5, price: 390, price_1: '380', date: '7.06'},
            {id: 6, price: 395, price_1: '380', date: '7.07'},
            {id: 7, price: 375, price_1: '380', date: '7.08'},
            {id: 8, price: 370, price_1: '380', date: '7.09'},
            {id: 8, price: 370, price_1: '380', date: '7.09'},
            {id: 8, price: 370, price_1: '380', date: '7.09'},
            {id: 8, price: 370, price_1: '380', date: '7.09'},
            {id: 8, price: 370, price_1: '380', date: '7.09'},
            {id: 8, price: 370, price_1: '380', date: '7.09'},
        ],
        accept_price: [
            {id: 1, price: 390},
            {id: 2, price: 415},
            {id: 3, price: 410},
            {id: 4, price: 400},
            {id: 5, price: 415},
            {id: 6, price: 410},
            {id: 7, price: 390},
            {id: 8, price: 395},
        ],

         // 分页
        pageNo: 1,
        totalPage: 0,
    },

    /**
     * My Function
     */

     
    // 商品筛选
    onCilckShopGoodsFilter(item){
        let filterType = item.currentTarget.dataset.item.id
        this.setData({ filterActive: item.currentTarget.dataset.index, productList: [], pageNo: 1, totalPage: 0 })
        switch (filterType) {
            case 1:
                this.setData({ price_tab: false });
                this.setData({ filterType: -1 })
                this.requestGoldProduct()
                break;
            case 2:
                this.setData({ price_tab: false });
                this.setData({ filterType: 30 })
                this.requestGoldProduct(30)
                break;
            case 3:
                this.setData({ price_tab: false });
                this.setData({ filterType: 31 })
                this.requestGoldProduct()
                break;
            case 4:
                this.setData({ price_tab: false });
                this.setData({ filterType: 32 })
                this.requestGoldProduct(32)
                break;
            case 5:
                this.setData({ price_tab: false });
                this.setData({ filterType: 33 })
                this.requestGoldProduct()
                break;
            case 6:
                this.setData({ price_tab: false });
                this.setData({ filterType: 34 })
                this.requestGoldProduct()
                break;
            case 7:
                this.setData({ price_tab: false });
                this.setData({ filterType: 35 })
                this.requestGoldProduct()
                break;
            case 8:
                this.setData({ price_tab: true });
                break;
            default:
                break;
        }
    },

    // 获取产品 getIdGlodShop
    requestGoldProduct() {
        let status = 1
        let shop_id = this.data.shop_id
        let type = this.data.filterType
        let data = { status, shop_id, type, pageNo : this.data.pageNo }
        getGoldProduct(res => {
            let request = res.data.data
            if (res.statusCode == 200) {
                this.setData({ productList: this.data.productList.concat(request), totalPage : res.data.totalPage })
            }
        }, data);
    },

    // 获取内容
    getPost() {
        let shop_id = this.data.shop_id
        let data = { shop_id }
        getIdGlodShop(res => {
            if (res.statusCode == 200) {
                if (res.data.favorite != null) {
                    this.setData({ isLike: true })
                }
                this.setData({ goldShopCon: res.data })
                this.requestGoldProduct()
            }
        }, data);
    },

    // 获取产品
    getGoldGoods() {

    },

    // 产品详细
    bottom_alert(e) {
        this.setData({ info: e.currentTarget.dataset.item })
        this.setData({ show: true })
    },

    // 关闭产品详细
    close() {
        this.setData({ show: false })
    },

    // 收藏
    onLike() {
        let user_id = wx.getStorageSync('uid')
        if (!user_id) {
            app.login()
            return
        }
        let data = { target_id: parseInt(this.data.shop_id, 10), type: 23 }

        if(this.data.isLike) {
            let data = { postId: this.data.goldShopCon.favorite.id }
            wx.request({
                url: `${API_URL}/favoritesDestroy`,
                data: data,
                header: app.header(),
                method: 'post',
                success: res=> {
                    if(res.statusCode == 200) {
                        this.setData({ isLike: false })
                        wx.showToast({
                            title: 'ok',
                            duration: 2000
                        })
                    }
                }
            })
        } else {
            wx.request({
                url: `${API_URL}/favorites`,
                data: data,
                header: app.header(),
                method: 'post',
                success: res=> {
                    if(res.statusCode == 200) {
                        let favorite = {
                            id : res.data.insertId
                        }
                        let detailed = this.data.goldShopCon
                        detailed.favorite = favorite
                        this.setData({ isLike: true, goldShopCon: detailed })
                        wx.showToast({
                            title: 'ok',
                            duration: 2000
                        })
                    }
                }
            })
        }
    },

    // 打电话联系
    onContact() {
        wx.makePhoneCall({
            phoneNumber: this.data.goldShopCon.phoneNum,
            fail: cancel => {
                console.log("cancel: ", cancel);
            }
        })
    },

    // 预览图
    preview(event) {
        let src = event.currentTarget.dataset.src.fullUrl;
        let previewImage = this.data.goldShopCon.images;
        let urls = []
        for (let item of previewImage) {
            urls.push(item.fullUrl)
        }
        wx.previewImage({
            current: src, // 当前显示图片的http链接
            urls: urls // 需要预览的图片http链接列表
        })
    },

     // 产品预览图
     goodsPreview(item) {
        let src = item.currentTarget.dataset.src.fullUrl;
        let imageArrList = []
        for (let imageArr of item.currentTarget.dataset.list) {
            imageArrList.push(imageArr.fullUrl)
        }
        wx.previewImage({
            current: src, // 当前显示图片的http链接
            urls: imageArrList// 需要预览的图片http链接列表
        })
    },

    // 价格激活 tab
    click_price_tab_1() {
        this.setData({ price_tab_active: 0 })
    },
    click_price_tab_2() {
        this.setData({ price_tab_active: 1 })
    },

    /**
     * App Function
     */

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({ lang: app.globalData.lang, navTitle: "goodDetailed", shop_id: options.shop_id })
        this.getPost()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if(this.data.pageNo == this.data.totalPage || this.data.totalPage == 0) return
        let pageNo = this.data.pageNo += 1
        this.setData({pageNo : pageNo})
        this.requestGoldProduct();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    /**
     * 朋友圈分享
     */
    onShareTimeline: function () {
        return {
            imageUrl: `${this.data.goldShopCon.images[0].fullUrl}`,
            title: `${ this.data.lang.dir == "rtl" ? this.data.goldShopCon.name_ug : this.data.goldShopCon.name_zh }`,
            path: '/pages/home/gold/goldProduct/goldProduct?shop_id=' + this.data.shop_id
        }
    }
})